package com.cmict.data.form.api.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.form.api.enums.DefaultNumber;
import com.cmict.data.form.api.mapper.SysEditionMapper;
import com.cmict.data.form.api.service.SysCurrencyService;
import com.cmict.data.form.api.service.SysEditionService;
import com.cmict.data.form.api.service.SysFormService;
import com.cmict.data.system.api.dict.DictDataApi;
import com.cmict.data.system.api.encodedetails.dict.EnCodeDataApi;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 文件夹表 服务实现类
 *
 * @Author qiaoyu
 * @since 2022-9-2
 */
@Service("sysEditionService")
@RequiredArgsConstructor
@Slf4j
public class SysEditionServiceImpl extends ServiceImpl<SysEditionMapper, Object> implements SysEditionService, DefaultNumber {

	private static final Logger logger = LoggerFactory.getLogger(SysEditionServiceImpl.class);

	private final SysEditionMapper sysEditionMapper;

	@Resource
	private SysFormService sysFormService;

	@Resource
	private DictDataApi dictDataApi;

	@Resource
	private SysCurrencyService sysCurrencyService;

	@Resource
	private EnCodeDataApi enCodeDataApi;


	@Override
	public CommonResult dataVersionRecord(String tableName) {
		String sql = "CREATE TABLE "+ tableName +"history SELECT * FROM "+ tableName +" WHERE 1=2";
		Boolean create = sysEditionMapper.dataVersionRecord(sql);
		if(create){
          String sqlafter = "ALTER TABLE "+ tableName +" ADD COLUMN table_name VARCHAR(100) COMMENT '属于哪张表的记录表',ADD COLUMN data_id bigint(100) COMMENT '属于那个数据的版本'";
          Boolean after = sysEditionMapper.dataVersionRecord(sqlafter);
          if(after){
			  return CommonResult.success("创建成功");
          }
		}
		return CommonResult.success("创建失败");
	}

    @Override
    public CommonResult getDataVersionList(String tableName, Long id) {
	    String sql = "SELECT * FROM "+ tableName +" WHERE data_id = '" +id +" ' ORDER BY create_time DESC ";
	    List<Map<Object,Object>> dataList = sysEditionMapper.getDataVersionList(sql);
        return CommonResult.success("获取成功",dataList);
    }
}
